package com.qili.controller.sys;

import cn.hutool.core.util.IdUtil;
import com.qili.entity.sys.SysUserProject;
import com.qili.entity.sys.SysUserWorkExperience;
import com.qili.service.SysUserService;
import com.qili.service.sys.SysUserProjectService;
import com.qili.service.sys.SysUserWorkExperienceService;
import com.qili.util.CommonUtil;
import com.qili.util.DateUtil;
import com.qili.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuyang
 * @date 2020/12/15
 * @description
 */
@Controller
@RequestMapping("/workEx")
public class SysUserWorkExperienceController {

    @Autowired
    private SysUserWorkExperienceService userWorkExperienceService;
    @Autowired
    private SysUserProjectService userProjectService;
    @Autowired
    private SysUserService userService;

    @RequestMapping("/saveWorks")
    @ResponseBody
    public JsonUtil saveWorkExperience(@RequestBody ArrayList<SysUserWorkExperience> experienceList){
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setFlag(false);

        if(experienceList.size()>0){
            for(SysUserWorkExperience experience : experienceList){
                String suweId = experience.getSuweId();
                if(StringUtils.isNotBlank(suweId)){
                    SysUserWorkExperience dbSysUserWorkExperience = userWorkExperienceService.selectByPrimaryKey(experience);
                    if(dbSysUserWorkExperience == null){
                        experience.setSuId(CommonUtil.getUserId());
                        experience.setCreateDate(DateUtil.getCurrentDateTimeString());
                        userWorkExperienceService.insertSelective(experience);
                    }else{
                        //更新工作经历
                        userWorkExperienceService.updateByPrimaryKeySelective(experience);
                    }
                }else{
                    experience.setSuweId(IdUtil.simpleUUID());
                    experience.setSuId(CommonUtil.getUserId());
                    userWorkExperienceService.insertSelective(experience);
                    jsonUtil.setStatus(1);
                }
                updateOrSaveUserProject(experience);
                deleteWorkExperience(experience);
                deleteProject(experience);
            }
        }

        //更改用户审核状态为待审核
        userService.updateUserAuditStatus(CommonUtil.getUserId(),"3",null);
        jsonUtil.setFlag(true);
        return jsonUtil;
    }

    @RequestMapping("/saveWork")
    @ResponseBody
    public JsonUtil saveWorkExperience(@RequestBody SysUserWorkExperience experience){
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setFlag(false);
        String suweId = experience.getSuweId();
        if(StringUtils.isNotBlank(suweId)){
            SysUserWorkExperience dbSysUserWorkExperience = userWorkExperienceService.selectByPrimaryKey(experience);
            if(dbSysUserWorkExperience == null){
                experience.setSuId(CommonUtil.getUserId());
                experience.setCreateDate(DateUtil.getCurrentDateTimeString());
                userWorkExperienceService.insertSelective(experience);
            }else{
                //更新工作经历
                userWorkExperienceService.updateByPrimaryKeySelective(experience);
            }
        }else{
            experience.setSuweId(IdUtil.simpleUUID());
            experience.setSuId(CommonUtil.getUserId());
            userWorkExperienceService.insertSelective(experience);
            jsonUtil.setStatus(1);
        }
        updateOrSaveUserProject(experience);
        deleteWorkExperience(experience);
        deleteProject(experience);
        jsonUtil.setFlag(true);
        return jsonUtil;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonUtil delete(SysUserWorkExperience experience){
        if(experience!=null && StringUtils.isNotBlank(experience.getSuweId())){
            userWorkExperienceService.deleteByPrimaryKey(experience);
            SysUserProject sysUserProject = new SysUserProject();
            sysUserProject.setSuweId(experience.getSuweId());
            userProjectService.delete(sysUserProject);
        }
        return JsonUtil.sucess("操作成功");
    }

    @RequestMapping("/deleteProj")
    @ResponseBody
    public JsonUtil deleteProj(SysUserProject project){
        if(project!=null && StringUtils.isNotBlank(project.getSupId())){
            userProjectService.deleteByPrimaryKey(project);
        }
        return JsonUtil.sucess("操作成功");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public JsonUtil deleteAll(){
        SysUserProject sysUserProject = new SysUserProject();
        sysUserProject.setSuId(CommonUtil.getUserId());
        userProjectService.delete(sysUserProject);
        SysUserWorkExperience experience = new SysUserWorkExperience();
        experience.setSuId(CommonUtil.getUserId());
        userWorkExperienceService.delete(experience);
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setFlag(true);
        return jsonUtil;
    }


    @RequestMapping("/projectList")
    @ResponseBody
    public List<SysUserProject> selectProjectListByWorkId(SysUserProject sysUserProject){
        List<SysUserProject> sysUserProjects = new ArrayList<>();
        if(sysUserProject != null && StringUtils.isNotBlank(sysUserProject.getSuweId())){
            Example userProjectExample = new Example(SysUserProject.class);
            userProjectExample.and().andEqualTo("suweId",sysUserProject.getSuweId());
            userProjectExample.setOrderByClause("table_ind asc");
            sysUserProjects = userProjectService.selectByExample(userProjectExample);
        }
        return sysUserProjects;
    }

    /**
     * 如果SysUserProject supId不为空,更新,为空,新增
     * @param experience
     */
    public void updateOrSaveUserProject(SysUserWorkExperience experience){
        List<SysUserProject> userProjectList = experience.getUserProjectList();
        if(null != userProjectList && userProjectList.size()>0){
            for(SysUserProject sysUserProject : userProjectList){
                if(StringUtils.isBlank(sysUserProject.getSupId())){
                    sysUserProject.setSuweId(experience.getSuweId());
                    sysUserProject.setSupId(IdUtil.simpleUUID());
                    sysUserProject.setSuId(CommonUtil.getUserId());
                    userProjectService.insertSelective(sysUserProject);
                }else{
                    SysUserProject dbProject = userProjectService.selectByPrimaryKey(sysUserProject);
                    if(dbProject == null){
                        sysUserProject.setSuId(CommonUtil.getUserId());
                        sysUserProject.setSuweId(experience.getSuweId());
                        sysUserProject.setCreateDate(DateUtil.getCurrentDateTimeString());
                        userProjectService.insertSelective(sysUserProject);
                    }else{
                        userProjectService.updateByPrimaryKeySelective(sysUserProject);
                    }
                }
            }
        }
    }


    /**
     * 删除工作经历及相关的项目
     * @param experience
     */
    public void deleteWorkExperience(SysUserWorkExperience experience){
        List<String> deledWorkExIds = experience.getDeledWorkExIds();
        if(null != deledWorkExIds && deledWorkExIds.size()>0){
            for(String workExId : deledWorkExIds){
                if(StringUtils.isNotBlank(workExId)){
                    SysUserProject sysUserProject = new SysUserProject();
                    sysUserProject.setSuweId(workExId);
                    userProjectService.delete(sysUserProject);
                    userWorkExperienceService.deleteByPrimaryKey(workExId);
                }
            }
        }
    }

    /**
     * 删除用户的项目
     * @param experience
     */
    public void deleteProject(SysUserWorkExperience experience){
        List<String> deleteProjectIds = experience.getDeledProjectIds();
        if(null != deleteProjectIds && deleteProjectIds.size()>0){
            for(String projectId : deleteProjectIds){
                userProjectService.deleteByPrimaryKey(projectId);
            }
        }
    }


}
